package com.chazz.dao.impl;

import com.chazz.dao.AdressDao;
import com.chazz.entity.Adress;
import com.chazz.utils.DBUtils;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;


public class AdressDaoImpl implements AdressDao {

    @Override
    public int addAdress(Adress adress) {
        //获取用户ID
        int uid=adress.getUid();
        //获取地址
        String uAdress=adress.getAdressvalue();
        String sql="insert into adress(uid,adressvalue) value(?,?)";
        Object[] parama={uid,uAdress};
        //插入并获取返回受影响行数
        int rt=DBUtils.executeUpdate(sql,parama);
        return rt;
    }

    @Override
    public int deletAdress(Adress adress) {
        int id=adress.getId();
        String sql="delete from adress where id=?";
        Object[] parama={id};
        int rt=DBUtils.executeUpdate(sql,parama);
        return rt;
    }

    @Override
    public List<Adress> findAllAdressByUid(int uid) throws SQLException {
        String sql="select * from adress where uid=?";
        ResultSet rs=DBUtils.executeQuery(sql,uid);
        List<Adress> adressList=new ArrayList<>();
        while(rs.next()){
            Adress adress=new Adress();
            adress.setId(rs.getInt("id"));
            adress.setUid(rs.getInt("uid"));
            adress.setAdressvalue(rs.getString("adressvalue"));
            adressList.add(adress);
        }
        return adressList;
    }

    @Override
    public int updateAdress(Adress adress) {
    int id=adress.getId();
    String uAdress=adress.getAdressvalue();
    String sql="update adress set adressvalue=? where id=?";
    Object[] parama={uAdress,id};
    int rt=DBUtils.executeUpdate(sql,parama);
    return rt;
    }
}


class test {

    public static void main(String[] args) {
        try {
            List<Adress> adressList = new AdressDaoImpl().findAllAdressByUid(1);
            for (Adress adress : adressList) {
                System.out.println(adress);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }
    }